<?php
declare (strict_types=1);

namespace app\controller;

use app\helpers\traits\SearchDataForModel;
use app\model\Statistics as ModelStatistics;
use app\BaseController;
use think\Request;

class Statistics extends BaseController
{
    use SearchDataForModel;

    /**
     * @User: 刘海龙
     * @Date: 2021/8/11
     * @Time: 16:35
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 个人统计
     */
    public function personalStatisticsList()
    {
        $limit = $this->request->param('limit', 10);
        $map = [];
        if ($this->request->has('user_id') && !empty($this->request->param('user_id'))) {
            $map[] = ['user_id', '=', $this->request->param('user_id')];
        }
        if ($this->request->has('review_project_cycle') && !empty($this->request->param('review_project_cycle'))) {
            $map[] = ['review_project_cycle', '=', $this->request->param('review_project_cycle')];
        }
        if ($this->request->has('community_id') && !empty($this->request->param('community_id'))) {
            $map[] = ['community_id', '=', $this->request->param('community_id') ];
        }
        $res = ModelStatistics::with(['users' => ['countCase'], 'community'])->where($map)->paginate($limit)->each(function ($item, $key) {
            $data = $item['users']['countCase']->toArray();
            $un_audit_count = 0; //未审核
            $audit_count = 0; //已审核
            foreach ($data as $k => $v) {
                if ($v['audit_status'] === 2) { //未审核的
                    $audit_count++;
                } else {
                    $un_audit_count++;
                }
            }
            $item['countCase'] = count($data) ?? 0;
            $item['audit_count'] = $audit_count ?? 0;
            $item['un_audit_count'] = $un_audit_count ?? 0;
            return $item;
        });
        return $this->response($res);
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/8/10
     * @Time: 14:31
     * @return \think\response\Json
     * 社区统计
     */
    public function communityStatisticsList()
    {
        $limit = $this->request->param('limit', 10);
        $map = [];
        if ($this->request->has('review_project_cycle') && !empty($this->request->param('review_project_cycle'))) {
            $map[] = ['review_project_cycle', '=', $this->request->param('review_project_cycle')];
        }
        if ($this->request->has('community_id') && !empty($this->request->param('community_id'))) {
            $map[] = ['community_id', '=', $this->request->param('community_id') ];
        }
        $res = ModelStatistics::with(['community' => ['samples' => ['cases']]])->field('review_project_id,review_project_cycle,community_id,sum(finish) as countFinish,sum(unfinished) as countUnfinished,sum(rebound) as countRebound,sum(disqualification) as countDisqualification')->where($map)->group('review_project_id,review_project_cycle,community_id')->paginate($limit)->each(function ($item, $key) {
            $data = $item['community']['samples']->toArray();
            $sumCase = 0;
            $no_review_case_count = 0; //未复查案件统计
            $review_case_count = 0; //已复查案件统计
            $un_audit_count = 0; //未审核案件统计
            $audit_count = 0; //已审核案件统计
            foreach ($data as $k => $v) {
                if (count($v['cases']) > 0) {
                    foreach ($v['cases'] as $k1=>$v1){
                        if($v1['review_status'] === 1){ //未复查
                            $no_review_case_count++;
                        }else{ //已复查
                            if ($v1['audit_status'] === 1){ //未审核
                                $un_audit_count++;
                            }else{ //已审核
                                $audit_count++;
                            }
                            $review_case_count++;
                        }
                    }
                    $sumCase += count($v['cases']);
                }
            }
            $item['countCase'] = $sumCase; //案件总数
            $item['audit_count'] = $audit_count ?? 0; //已审核案件总数
            $item['un_audit_count'] = $un_audit_count ?? 0; //未复查案件总数
            $item['no_review_case_count'] = $no_review_case_count ?? 0; //未复查案件总数
            $item['review_case_count'] = $review_case_count ?? 0;//已复查案件总数
            return $item;
        });
        return $this->response($res);
    }


}
